﻿using Device.API.Applcation.Command.EquipmentCommand;
using Device.Infrstructre.Interface;
using MediatR;

namespace Device.API.Applcation.CommandHandlers.EquipmentCommandHandlers
{
    public class CreateEquipmentCommandHandlers:IRequestHandler<CreateEquipmentCommand, int>
    {

        private readonly IEquipmentRepository _equipment;

        public CreateEquipmentCommandHandlers(IEquipmentRepository equipment)
        {
            _equipment = equipment;
        }

        public async Task<int> Handle(CreateEquipmentCommand request, CancellationToken cancellationToken)
        {

            var result = await _equipment.GetAllAsync();
            if(result.Any(x=>x.EquipmentName == request.EquipmentName))
            {
                
                return -1;
            }
            if(result.Any(x=>x.EquipmentCode == request.EquipmentCode))
            {
                return -2;
            }


            return await _equipment.AddAsync(new Domain.EquipmentModel() { AddName = request.AddName, EquipmentCode = request.EquipmentCode,EquipmentId = request.EquipmentId, EquipmentName = request.EquipmentName, AuthorIdentifier = request.AuthorIdentifier, IsAuthorize = request.IsAuthorize, EquipmentState = request.EquipmentState, Eremark = request.Eremark, AddTime = request.AddTime,Office_Id = request.Office_Id, User_Id= request.User_Id});
        }
    }
}
